package com.park.server.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;


/**
 * @description 上传图片
 * @author caizh
 * @email caizh521@sina.cn
 * @date 2014-6-3 上午9:42:54
 */
public class UploadImage {
    public static Logger log = LoggerFactory.getLogger(UploadImage.class);


    public static String saveUploadedFile(MultipartFile file, String folderPath) throws IOException {
        // 保存file
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
        SimpleDateFormat time = new SimpleDateFormat("HHmmss");
        String folder = sdf.format(d);

        String relativePath = File.separator + folder + File.separator;
        // String path = this.evidencePath + File.separator + folder +
        // File.separator;
        log.debug(folderPath + File.separator + relativePath);
        File f = new File(folderPath + File.separator + relativePath);
        if (!(f.exists())) {
            f.mkdirs();
        }
        log.debug("..................." + file.getOriginalFilename());
        StringBuffer sb = new StringBuffer();
        sb.append(folder);// 新建的文件夹名称，子路径
        sb.append(File.separator);// 分隔符'/'
        String imgName = time.format(d) + "_" + System.currentTimeMillis() + ".jpg"; // 文件名称,目前只上传jpg图片
        sb.append(imgName);
        File dest = new File(folderPath + sb.toString());
        file.transferTo(dest);
        log.debug("图片路径：" + folder + "/" + imgName);
        return folder + "/" + imgName;
    }
}
